function email_compose(){
	queryURL( BASE_URI+'admin/dataman/subscriptions/email_compose_form' );
}

function sms_compose(){
	queryURL( BASE_URI+'admin/dataman/subscriptions/sms_compose_form' );
}

function edit_email(id){
	queryURL( BASE_URI+'admin/dataman/subscriptions/email_compose_form?id='+id );
}

function delete_email(id){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/delete_email',{id:id},function(res){
		reload();
	});
}

function delete_sms(id){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/delete_sms',{id:id},function(res){
		reload();
	});
}

function send_email(id){
	jQuery('#loader_image_'+id).show();
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/send_email',{id:id},function(res){
		jQuery('#loader_image_'+id).hide();
		reload();
	});
}

function edit_sms(id){
	queryURL( BASE_URI+'admin/dataman/subscriptions/sms_compose_form?id='+id );
}

function send_sms(id){
	jQuery('#loader_image_'+id).show();
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/send_sms',{id:id},function(res){
		jQuery('#loader_image_'+id).hide();
		reload();
	});
}

function compose_save(id){
	jQuery('#loader_image').show();
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/email_compose_form_submit',{id:id, subject:jQuery('#subject').val(), body:jQuery('#body').val()},function(res){
		jQuery('#loader_image').hide();
		if(res == 'ok')
			queryURL( BASE_URI+'admin/dataman/subscriptions/email_compose' );
		else
			alert(res);
	});
}

function sms_compose_save(id){
	jQuery('#loader_image').show();
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/sms_compose_form_submit',{id:id, content:jQuery('#sms_content').val()},function(res){
		jQuery('#loader_image').hide();
		if(res == 'ok')
			queryURL( BASE_URI+'admin/dataman/subscriptions/sms_compose' );
		else
			alert(res);
	});
}

function sms_compose_send(id){
	jQuery('#loader_image').show();
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/sms_compose_form_send',{id:id, content:jQuery('#sms_content').val()},function(res){
		jQuery('#loader_image').hide();
		if(res == 'ok')
			queryURL( BASE_URI+'admin/dataman/subscriptions/sms_compose' );
		else
			alert(res);
	});
}

function add_user_email_subs(){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/add_new_user_email_subs',{},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Add email subscription' 
			}
		);
	});	
}

function add_user_sms_subs(){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/add_new_user_sms_subs',{},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:200,
				 title: 'Add SMS subscription' 
			}
		);
	});	
}

function unsubscription_user_email(user_id){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/unsubscription_user_email',{user_id:user_id},function(res){
		reload();
	});
}

function unsubscription_user_sms(user_id){
	jQuery.post(BASE_URI+'admin/dataman/subscriptions/unsubscription_user_sms',{user_id:user_id},function(res){
		reload();
	});
}

jQuery(document).ready(function() {
	var optionsEmailSubs = { 
		beforeSubmit:beforeSubmitEmail,   
		success:afterSubmitEmail
	};	
	var optionsSMSSubs = { 
		beforeSubmit:beforeSubmitSMS,   
		success:afterSubmitSMS
	};	
	jQuery("#email_subs_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsEmailSubs);
		return false;
	});	
	jQuery("#sms_subs_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsSMSSubs);
		return false;
	});	
	
});

function afterSubmitSMS(responseText, statusText, xhr, $form){
	jQuery('#loader_image').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
}

function beforeSubmitSMS(formData, jqForm, options){
	jQuery('#loader_image').show();
	return true;
}	

function afterSubmitEmail(responseText, statusText, xhr, $form){
	jQuery('#loader_image').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
} 

function beforeSubmitEmail(formData, jqForm, options){
	jQuery('#loader_image').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'email']; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader_image').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
}